#include "../stdafx.h"
#include "ResConfig.h"
#include "Util.h"
#include "Config.h"
#include "../debug_new.h"

CResConfig::CResConfig(void)
{
	m_bFromDll = FALSE;
}

CResConfig::~CResConfig(void)
{

}

BOOL CResConfig::InitFromFile(wstring strFile)
{
	wstring strPath = CUtil::GetCurrentDirectory();
	strPath += L"\\Config\\";
	strPath += strFile;
	if(!m_xml.Load(strPath))
	{
		return FALSE;
	}

	return TRUE;
}

BOOL CResConfig::Init(wstring strFile)
{
#ifdef XML_FROM_FILE

	wstring strPath = CUtil::GetCurrentDirectory();
	strPath += L"\\Config\\";
	strPath += CConfig::GetInstance()->GetSkin();
	strPath += L"\\";
	strPath += strFile;

	if(!m_xml.Load(strPath))
	{
		return FALSE;
	}

	m_xml.ResetMainPos();
	if( m_xml.FindElem(L"Setting") )
		m_strResPath = m_xml.GetAttrib(L"ImgPath");

	return TRUE;
#else

	BOOL bRet = FALSE;

	bRet = CUtil::InitResXML(m_xml,strFile,L"XML");

	return bRet;

#endif
}

BOOL CResConfig::GetBtnParam(BtnParam &btnInfo,wstring strkey)
{
	m_xml.ResetMainPos();
	btnInfo.m_rc.SetRectEmpty();
	if(m_xml.FindChildElem(strkey))
	{
		wstring strTmp = m_xml.GetChildAttrib(L"IMAGE");
		if(!strTmp.length())
		{
			return FALSE;
		}
		else
		{
			btnInfo.m_strImg = strTmp;
		}

		strTmp = m_xml.GetChildAttrib(L"RECT");
		btnInfo.m_rc = GetRectFromString(strTmp);

		strTmp = m_xml.GetChildAttrib(L"NTC");
		btnInfo.m_clrTxtNormal = GetColorFromString(strTmp);

		strTmp = m_xml.GetChildAttrib(L"ATC");
		btnInfo.m_clrTxtActive = GetColorFromString(strTmp);

		strTmp  = m_xml.GetChildAttrib(L"TOS");
		btnInfo.m_szTxtOffset = GetSizeFormString(strTmp);

		strTmp  = m_xml.GetChildAttrib(L"BETRANS");
		if(strTmp != L"0")
		{
			btnInfo.m_bTranparent = TRUE;
		}
		else
		{
			btnInfo.m_bTranparent = FALSE;
		}

		strTmp = m_xml.GetChildAttrib(L"TCOLOR");
		btnInfo.m_clrTranparent = GetColorFromString(strTmp);

		strTmp = m_xml.GetChildAttrib(L"ALIGN");
		btnInfo.m_dwAlign = _wtoi(strTmp.c_str());

		strTmp = m_xml.GetChildAttrib(L"ICONNUM");
		btnInfo.m_nImgNum = _wtoi(strTmp.c_str());

		btnInfo.m_strFont = m_xml.GetChildAttrib(L"FONT");
	}
	else
	{
		////RETAILMSG (APDEBUG, (TEXT("GetBtnParam Failed !\r\n")));
		return FALSE;
	}
	
	return TRUE;
}

BOOL CResConfig::GetTextParam(TextParam &txtInfo,wstring strKey)
{
	m_xml.ResetMainPos();
	if(m_xml.FindChildElem(strKey))
	{
		wstring strTmp;
	
		strTmp = m_xml.GetChildAttrib(L"RECT");
		txtInfo.m_rc = GetRectFromString(strTmp);

		strTmp = m_xml.GetChildAttrib(L"COLOR");
		txtInfo.m_clrText = GetColorFromString(strTmp);

		strTmp = m_xml.GetChildAttrib(L"GCOLOR");
		txtInfo.m_clrGrow = GetColorFromString(strTmp);

		strTmp = m_xml.GetChildAttrib(L"ALIGN");
		txtInfo.m_dwAlign = _wtoi(strTmp.c_str());


		txtInfo.m_strLangKey  = m_xml.GetChildAttrib(L"LANGKEY");
		txtInfo.m_strFont = m_xml.GetChildAttrib(L"FONT");
	}
	else
	{
		////RETAILMSG (APDEBUG, (TEXT("GetTextParam Failed !\r\n")));
		return FALSE;
	}

	return TRUE;
}

BOOL CResConfig::GetBatTimePicParam(BatTimePicParam &battimeInfo,wstring strKey)
{
	m_xml.ResetMainPos();
	if(m_xml.FindChildElem(strKey))
	{
	
		battimeInfo.m_strImg = m_xml.GetChildAttrib(L"IMAGE");;
		wstring strTmp = m_xml.GetChildAttrib(L"RECT");
		battimeInfo.m_rc = GetRectFromString(strTmp);

		strTmp  = m_xml.GetChildAttrib(L"BETRANS");
		if(strTmp != L"0")
		{
			battimeInfo.m_bTranparent = TRUE;
		}
		else
		{
			battimeInfo.m_bTranparent = FALSE;
		}

		strTmp = m_xml.GetChildAttrib(L"TCOLOR");
		battimeInfo.m_clrTranparent = GetColorFromString(strTmp);

	}
	else
	{
		////RETAILMSG (APDEBUG, (TEXT("GetBatTimePicParam Failed !\r\n")));
		return FALSE;
	}

	return TRUE;
}

BOOL CResConfig::GetProgreeParam(ProgressParam &progressInfo,wstring strKey)
{
	m_xml.ResetMainPos();
	if(m_xml.FindChildElem(strKey))
	{
		wstring strTmp = m_xml.GetChildAttrib(L"CHANELN");
		if(!strTmp.length())
		{
			return FALSE;
		}
		else
		{
			progressInfo.m_strChanelNormal = strTmp;
		}

		strTmp = m_xml.GetChildAttrib(L"CHANELA");
		if(!strTmp.length())
		{
			return FALSE;
		}
		else
		{
			progressInfo.m_strChanelActive = strTmp;
		}

		progressInfo.m_strThumbNormal = m_xml.GetChildAttrib(L"THUMBN");
		//if(!strTmp.length())
		//	return FALSE;
		//else
		//	progressInfo.m_strThumbNormal = strTmp;
		

		progressInfo.m_strThumbActive = m_xml.GetChildAttrib(L"THUMBA");
		//if(!strTmp.length())
		//{
		//	return FALSE;
		//}
		//else
		//{
		//	progressInfo.m_strThumbActive = strTmp;
		//}

		strTmp = m_xml.GetChildAttrib(L"RECT");
		progressInfo.m_rc = GetRectFromString(strTmp);

		strTmp  = m_xml.GetChildAttrib(L"MARGIN");
		progressInfo.m_szMargin = GetSizeFormString(strTmp);

		strTmp  = m_xml.GetChildAttrib(L"BETRANS");
		if(strTmp != L"0")
		{
			progressInfo.m_bTranparent = TRUE;
		}
		else
		{
			progressInfo.m_bTranparent = FALSE;
		}

		strTmp = m_xml.GetChildAttrib(L"TCOLOR");
		progressInfo.m_clrTranparent = GetColorFromString(strTmp);

		strTmp  = m_xml.GetChildAttrib(L"BVER");
		if(strTmp != L"0")
		{
			progressInfo.m_bVer = TRUE;
		}
		else
		{
			progressInfo.m_bVer = FALSE;
		}		
		strTmp  = m_xml.GetChildAttrib(L"ENABLE");
		if(strTmp != L"0")
		{
			progressInfo.m_bEnable = TRUE;
		}
		else
		{
			progressInfo.m_bEnable = FALSE;
		}
	}
	else
	{
		////RETAILMSG (APDEBUG, (TEXT("GetProgreeParam Failed !\r\n")));
		return FALSE;
	}

	return TRUE;
}

BOOL CResConfig::GetListParam(ListParam &listParam,wstring strKey)
{
	m_xml.ResetMainPos();
	if(m_xml.FindChildElem(strKey))
	{
		wstring strTmp;

		strTmp = m_xml.GetChildAttrib(L"RECT");
		listParam.m_rc = GetRectFromString(strTmp);

		strTmp = m_xml.GetChildAttrib(L"NTC");
		listParam.m_clrTxtNormal = GetColorFromString(strTmp);

		strTmp = m_xml.GetChildAttrib(L"ATC");
		listParam.m_clrTxtActive = GetColorFromString(strTmp);

		strTmp = m_xml.GetChildAttrib(L"ALIGN");
		listParam.m_dwAlign = _wtoi(strTmp.c_str());

		strTmp = m_xml.GetChildAttrib(L"IITEMH");
		listParam.m_nItemHight = _wtoi(strTmp.c_str());

		strTmp = m_xml.GetChildAttrib(L"STYLE");
		listParam.m_dwStyle = _wtoi(strTmp.c_str());

		listParam.m_strImg = m_xml.GetChildAttrib(L"IMAGE");

		listParam.m_strSelItem = m_xml.GetChildAttrib(L"IMAGESITEM");

		strTmp  = m_xml.GetChildAttrib(L"BETRANS");
		if(strTmp != L"0")
		{
			listParam.m_bTranparent = TRUE;
		}
		else
		{
			listParam.m_bTranparent = FALSE;
		}

		strTmp = m_xml.GetChildAttrib(L"TCOLOR");
		listParam.m_clrTranparent = GetColorFromString(strTmp);

		strTmp = m_xml.GetChildAttrib(L"LCLRSTART");
		listParam.m_clrLineStart = GetColorFromString(strTmp);

		strTmp = m_xml.GetChildAttrib(L"LCLREND");
		listParam.m_clrLineEnd = GetColorFromString(strTmp);

		strTmp  = m_xml.GetChildAttrib(L"ICONSZ");
		listParam.m_szIcon = GetSizeFormString(strTmp);

		listParam.m_strFont = m_xml.GetChildAttrib(L"FONT");
	}
	else
	{
		////RETAILMSG (APDEBUG, (TEXT("GetListParam Failed !\r\n")));
		return FALSE;
	}

	return TRUE;
}

BOOL CResConfig::Token(wstring &strInput,TCHAR* pStep,vector<wstring>& vRet)
{
	TCHAR *pToken;
	pToken = _tcstok( (TCHAR*)strInput.c_str(), pStep); 
	
	while( pToken != NULL )
	{
		vRet.push_back(wstring(pToken));

		pToken = _tcstok( NULL, pStep ); // C4996
	}

	return TRUE;
}

CRect CResConfig::GetRectFromString(wstring strRect)
{
	if(!strRect.length())
	{
		return NULL;
	}

	vector<wstring>  vRect;

	Token(strRect,L",",vRect);

	if(vRect.size() != 4)
	{
		////RETAILMSG (APDEBUG, (TEXT("GetRectFromString Failed !\r\n")));
		return NULL;
	}

	int nLeft   = _wtoi(vRect[0].c_str());
	int nTop	= _wtoi(vRect[1].c_str());
	int nWidth  = _wtoi(vRect[2].c_str());
	int nHeight = _wtoi(vRect[3].c_str());

	if(0 == nWidth || 0 == nHeight || 0 > nLeft || 0 > nTop)
	{
		////RETAILMSG (APDEBUG, (TEXT("GetRectFromString nLeft:%d, nTop:%d,nWidth:%d,nHeight:%d\r\n"),nLeft,nTop,nWidth,nHeight));
	}

	return CRect(nLeft,nTop,nLeft + nWidth,nTop + nHeight);
}

COLORREF CResConfig::GetColorFromString(wstring strColor)
{
	if(!strColor.length())
	{
		return NULL;
	}

	vector<wstring>  vRGB;

	Token(strColor,L",",vRGB);

	if(vRGB.size() != 3)
	{
		////RETAILMSG (APDEBUG, (TEXT("GetColorFromString Failed !\r\n")));
		return NULL;
	}

	return RGB(_wtoi(vRGB[0].c_str()),_wtoi(vRGB[1].c_str()),_wtoi(vRGB[2].c_str()));
}

CSize CResConfig::GetSizeFormString(wstring strSize)
{
	if(!strSize.length())
	{
		return NULL;
	}

	vector<wstring>  vSize;

	Token(strSize,L",",vSize);

	if(vSize.size() != 2)
	{
		////RETAILMSG (APDEBUG, (TEXT("GetSizeFormString Failed !\r\n")));
		return NULL;
	}

	return CSize(_wtoi(vSize[0].c_str()),_wtoi(vSize[1].c_str()));
}

wstring CResConfig::GetAttribData(wstring strKey,wstring strAttrib)
{
	if(!strKey.length())
	{
		return NULL;
	}

	if(!strAttrib.length())
	{
		return NULL;
	}

	m_xml.ResetMainPos();
	
	if(m_xml.FindChildElem(strKey))
	{
		return m_xml.GetChildAttrib(strAttrib);
	}
	else
	{
		////RETAILMSG (APDEBUG, (TEXT("GetAttribData Failed !\r\n")));
		return L"";
	}
}
BOOL CResConfig::GetVectorData(wstring strKey,vector<wstring> &vString)
{
	if(!strKey.length())
	{
		return FALSE;
	}

	m_xml.ResetMainPos();

	if(m_xml.FindChildElem(strKey))
	{
		m_xml.IntoElem();

		while (m_xml.FindChildElem(L"Data"))
		{
			vString.push_back(m_xml.GetChildData());
		}
	}
	else
	{
		////RETAILMSG (APDEBUG, (TEXT("GetVectorData Failed !\r\n")));
		return FALSE;
	}	

	m_xml.OutOfElem();
	m_xml.OutOfElem();

	return TRUE;
}

BOOL CResConfig::GetTabInfo(wstring strKey,vector<TabInfo> &m_vLan)
{
	if(!strKey.length())
	{
		return FALSE;
	}

	m_xml.ResetMainPos();

	if(m_xml.FindChildElem(strKey))
	{
		m_xml.IntoElem();

		while (m_xml.FindChildElem(L"Data"))
		{
			TabInfo lanInfo;
			lanInfo.m_strPara = m_xml.GetChildAttrib(L"PARA");
			lanInfo.m_strData  = m_xml.GetChildData();
			m_vLan.push_back(lanInfo);
		}
	}
	else
	{
		////RETAILMSG (APDEBUG, (TEXT("GetTabInfo Failed !\r\n")));
		return FALSE;
	}	

	m_xml.OutOfElem();
	m_xml.OutOfElem();

	return TRUE;
}


CRect CResConfig::GetRect()
{
	m_xml.ResetMainPos();
	m_xml.FindElem(L"Setting");
	return GetRectFromString(m_xml.GetAttrib(L"RECT"));
}

wstring CResConfig::GetName()
{
	m_xml.ResetMainPos();
	m_xml.FindElem(L"Setting");
	return m_xml.GetAttrib(L"NAME");
}

wstring CResConfig::GetImage()
{
	m_xml.ResetMainPos();
	m_xml.FindElem(L"Setting");
	return m_xml.GetAttrib(L"Img");
}

BOOL CResConfig::IsResFromDll()
{
	m_xml.ResetMainPos();
	m_xml.FindElem(L"Setting");
	if(L"0" == m_xml.GetAttrib(L"ResType"))
	{
		m_bFromDll =  FALSE;
	}
	else
	{
		m_bFromDll = TRUE;
	}

	return m_bFromDll;
}


void CResConfig::SetBtnParam(CBmpBtn *pBmpBtn,BtnParam &btnInfo)
{
	FONT_DECLARE;
	if(NULL == pBmpBtn)
	{
		////RETAILMSG (APDEBUG, (TEXT("SetBtnParam failed invalide button handle!\r\n")));
	}
	HBITMAP hBitmap = CUtil::LoadBitmapEx( m_strResPath+ btnInfo.m_strImg,m_bFromDll);
	if(NULL == hBitmap)
	{
		////RETAILMSG (APDEBUG, (TEXT("SetBtnParam bitmap failed : %s!\r\n"),btnInfo.m_strImg));
	}
	pBmpBtn->SetTextOffset(btnInfo.m_szTxtOffset);
	pBmpBtn->SetTxtNormal(btnInfo.m_clrTxtNormal);
	pBmpBtn->SetTxtActive(btnInfo.m_clrTxtActive);
	pBmpBtn->SetAlign(btnInfo.m_dwAlign);
	pBmpBtn->SetIconNum(btnInfo.m_nImgNum);
	pBmpBtn->SetFont(FNT_GETFONT(btnInfo.m_strFont));
	pBmpBtn->SetBmp(hBitmap,btnInfo.m_bTranparent,btnInfo.m_clrTranparent);
}

void CResConfig::SetTextParam(CText *pText,TextParam &txtInfo)
{
	FONT_DECLARE;
	pText->SetTextAlign(txtInfo.m_dwAlign);
	pText->SetTxtNormal(txtInfo.m_clrText);
	pText->SetTxtActive(txtInfo.m_clrGrow);
	pText->SetTextAlign(txtInfo.m_dwAlign);
	pText->SetFont(FNT_GETFONT(txtInfo.m_strFont));
}

void CResConfig::SetBatParam(CBatCtrl *pBatCtrl,BatTimePicParam &battimeInfo)
{
	HBITMAP hBitmap = CUtil::LoadBitmapEx( m_strResPath+battimeInfo.m_strImg,m_bFromDll);
	pBatCtrl->SetFrameNum(8);
	pBatCtrl->SetBmp(hBitmap,battimeInfo.m_bTranparent,battimeInfo.m_clrTranparent);
}

void CResConfig::SetTimeParam(CTimeCtrl *pTimeCtrl,BatTimePicParam &battimeInfo)
{
	HBITMAP hBitmap = CUtil::LoadBitmapEx( m_strResPath+battimeInfo.m_strImg,m_bFromDll);
	pTimeCtrl->SetBmp(hBitmap,battimeInfo.m_bTranparent,battimeInfo.m_clrTranparent);
	pTimeCtrl->ReCalculatorRect();
}

void CResConfig::SetPicParam(CPicture *pPicture,BatTimePicParam &picInfo)
{
	FONT_DECLARE;
	HBITMAP hBitmap = CUtil::LoadBitmapEx( m_strResPath+picInfo.m_strImg,m_bFromDll);
	pPicture->SetPic(hBitmap);

	pPicture->SetRect( picInfo.m_rc );
	pPicture->SetTransParam(picInfo.m_bTranparent,picInfo.m_clrTranparent);
}

void CResConfig::SetProgressParam(CProgress *pProgressInfo,ProgressParam &proInfo)
{
	HBITMAP hBitmap1,hBitmap2;

	hBitmap1 = (HBITMAP)CUtil::LoadBitmapEx( m_strResPath+proInfo.m_strChanelNormal,m_bFromDll);
	hBitmap2 = (HBITMAP)CUtil::LoadBitmapEx( m_strResPath+proInfo.m_strChanelActive,m_bFromDll);
	pProgressInfo->SetBitmapChannel(hBitmap1,hBitmap2,proInfo.m_bTranparent,proInfo.m_clrTranparent);

	hBitmap1 = (HBITMAP)CUtil::LoadBitmapEx( m_strResPath+proInfo.m_strThumbNormal,m_bFromDll);
	hBitmap2 = (HBITMAP)CUtil::LoadBitmapEx( m_strResPath+proInfo.m_strThumbActive,m_bFromDll);
	pProgressInfo->SetBitmapThumb(hBitmap1,hBitmap2,proInfo.m_bTranparent,proInfo.m_clrTranparent);
	pProgressInfo->SetVertical(proInfo.m_bVer); 
	pProgressInfo->SetMargin(proInfo.m_szMargin.cx,proInfo.m_szMargin.cy,proInfo.m_szMargin.cx,proInfo.m_szMargin.cy);
	pProgressInfo->Enable(proInfo.m_bEnable);
}

int CResConfig::GetAniType()
{
	wstring str = GetAttribData(L"Ani",L"Type");
	return _wtoi(str.c_str());
}

int CResConfig::GetExitAniType()
{
	wstring str = GetAttribData(L"Ani",L"ExitType");
	return _wtoi(str.c_str());
}

int CResConfig::GetAniSpeed()
{
	return _wtoi(GetAttribData(L"Ani",L"Speed").c_str());
}
